package com.idcheck.rechadfire;


import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.idcheck.utils.LogUtil;
import com.idcheck.utils.OtherUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;


public class CommUtil {
    /*public static void shutDownServerSocket() {
        try {
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/
}



class TcpServerThread extends Thread  {

    private int port;
    Socket client;
    private byte buffer[] = new byte[500];
    InputStream inputStream;
    OutputStream outputStream;

    //public static ServerSocket serverSocket;
    ServerSocket serverSocket;


    public TcpServerThread(int port){
        this.port = port;
    }

    @Override
    public void run() {
        super.run();

        try {
            serverSocket = new ServerSocket(port);

            //改为可重用地址
            /*serverSocket = new ServerSocket();
            serverSocket.setReuseAddress(true);
            serverSocket.bind(new InetSocketAddress(port));*/

        } catch (IOException e) {
            Log.d("server IOException:", e.getMessage().toString());
            LogUtil.getInstance().info(OtherUtil.getCurrentTime()+"server IOException:"+ e.getMessage().toString());
        }

        while (true){
            if (null != serverSocket) {
                try{
                    client = serverSocket.accept();
                    inputStream = client.getInputStream();
                    outputStream = client.getOutputStream();
                    //启动接收线程
                    Receive_Thread receive_Thread = new Receive_Thread();
                    receive_Thread.start();
                }
                catch (IOException e){
                    Log.d("client IOException:", e.getMessage().toString());
                    LogUtil.getInstance().info(OtherUtil.getCurrentTime()+"client IOException:"+ e.getMessage().toString());
                }
            }
        }
    }

    public void sendData(String msg){

        if (outputStream != null &&client != null && client.isConnected()){
            try {
                outputStream.write(msg.getBytes());

            } catch (IOException e) {
                Log.d("sendData IOException:", e.getMessage().toString());

                inputStream = null;
                outputStream = null;
                client = null;
            }
        }
    }
    public Boolean closeClient(){
        try {

            client.close();
            inputStream.close();
            outputStream.close();
            inputStream = null;
            outputStream = null;
            client = null;
        } catch (IOException e) {
            Log.d("sendData IOException:", e.getMessage().toString());
            inputStream = null;
            outputStream = null;
            client = null;
        }
        return true;
    }
    public void checkConnect(){
        try {

            if (client.isConnected()){
                ;
            }
            else{
                client.close();

            }
        } catch (IOException e) {
            Log.d("sendData IOException:", e.getMessage().toString());
            inputStream = null;
            outputStream = null;
            client = null;
        }
    }
    public Boolean sendPing(String msg){
        try {

            if (outputStream != null &&client != null && client.isConnected()){
                outputStream.write(msg.getBytes());
                return true;
            }
        } catch (IOException e) {
            Log.d("sendData IOException:", e.getMessage().toString());
            inputStream = null;
            outputStream = null;
            client = null;
        }
        return false;
    }

    /**
     *
     * 接收线程
     *
     */
    class Receive_Thread extends Thread//继承Thread
    {
        public void run()//重写run方法
        {
            while (true)
            {
                try
                {

                    final byte[] buf = new byte[1024];
                    final int len = inputStream.read(buf);
                    if (len != -1) {
                        final String text = new String(buf, 0, len);

                        Message msg = new Message();
                        String pageName = "";
                        if (text.contains("1")) {
                            msg.what = 0x21;
                            EventBus.getDefault().post(msg);
                        } else if (text.contains("2")) {
                            msg.what = 0x22;
                            EventBus.getDefault().post(msg);
                        } else if (text.contains("3")) {
                            msg.what = 0x23;
                            EventBus.getDefault().post(msg);
                        } else if (text.contains("0")) {
                            msg.what = 0x20;
                            EventBus.getDefault().post(msg);
                        }
                    }
                }
                catch (Exception e)
                {
                    // TODO Auto-generated catch block

                    e.printStackTrace();
                    break;
                }
            }
        }
    }
}